package com.example.administrator.wallet.view.activity.mine.Contacts;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.constant.Api;
import com.example.administrator.wallet.intf.IRequestCallback;
import com.example.administrator.wallet.intf.IRequestManager;
import com.example.administrator.wallet.model.bean.ContactBean;
import com.example.administrator.wallet.utils.RequestFactory;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.google.gson.Gson;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.example.administrator.wallet.R.id.ib_add;
import static com.example.administrator.wallet.R.id.tv_contact;
import static com.example.administrator.wallet.R.id.tv_contact_currency;

public class HasContactActivity extends AppCompatActivity {

    private ImageButton mIb_back, mIb_add;
    private TextView mV_title, mContact_currency;
    private ListView mLv_contact;
    private RelativeLayout mRl_nohas;
    private ImageButton mNohas_img;
    private Button mBt_add;
    private ContactBean contactBean;
    private List<ContactBean.DataBean> datas;
    private SimpleAdapter adapter = null;
    private String id = "";
    private int code = 0;
    private com.example.administrator.wallet.model.bean.ContactBean.DataBean dataBean;
    private ImageView selects;
    private List<ContactBean> data;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_has_contact);

        setStatuColor();
        mIb_back = (ImageButton) findViewById(R.id.finish_img);
        mV_title = (TextView) findViewById(R.id.view_title);
        mLv_contact = (ListView) findViewById(R.id.lv_contact);
        mIb_add = (ImageButton) findViewById(ib_add);
        mRl_nohas = (RelativeLayout) findViewById(R.id.rl_nohas);
        mNohas_img = (ImageButton) findViewById(R.id.nohas_img);
        mBt_add = (Button) findViewById(R.id.bt_add);
        mContact_currency = (TextView) findViewById(tv_contact_currency);


        datas = new ArrayList<>();
        //给ListView_main_wordsList注册上下文菜单
        registerForContextMenu(mLv_contact);


        final IRequestManager reqequestManager = RequestFactory.getReqequestManager();
        reqequestManager.httpPost(Api.BaseUrl + Api.Contact_List, Api.getContactList(SharedPrefsUtil.getValue(HasContactActivity.this, "wallet", "token", "")), new IRequestCallback() {
            @Override
            public void onSuccess(String Response) {
                Gson gson = new Gson();
                contactBean = gson.fromJson(Response, com.example.administrator.wallet.model.bean.ContactBean.class);
                code = contactBean.getCode();
                if (code == 200) {
                    datas.addAll(contactBean.getData());
                    List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
                    for (int i = 0; i < datas.size(); i++) {
                        HashMap<String, Object> map = new HashMap<String, Object>();
                        dataBean = datas.get(i);
                        map.put("name", dataBean.getName());
                        map.put("contact_currency", dataBean.getCoin_symbol());
                        id = dataBean.getId();
                        list.add(map);
                    }


                    final Intent intent = getIntent();
                    int seleces = intent.getIntExtra("selected", 0);
                    if (seleces == 0) {
                        adapter = new SimpleAdapter(HasContactActivity.this, list, R.layout.item_contact, new String[]{"name", "contact_currency"}, new int[]{tv_contact, tv_contact_currency});
//                         给ListView 设置适配器
                        mLv_contact.setAdapter(adapter);
                    } else if (seleces == 1) {
                        adapter = new SimpleAdapter(HasContactActivity.this, list, R.layout.item_contact_select, new String[]{"name", "contact_currency"}, new int[]{tv_contact, tv_contact_currency});
                        // 给ListView 设置适配器
                        mLv_contact.setAdapter(adapter);
                        mIb_add.setVisibility(View.GONE);


                        mLv_contact.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
                                View viewG = mLv_contact.getChildAt(i);
                                final ImageView sele = (ImageView) viewG.findViewById(R.id.select);
                                sele.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        sele.setSelected(true);
                                        String addr =datas.get(i).getWallet_addr();
                                        String coin_symbol = datas.get(i).getCoin_symbol();
                                        Intent resultInt = new Intent();
                                        resultInt.putExtra("addr",addr);
                                        resultInt.putExtra("coin_symbol",coin_symbol);
                                        setResult(101,resultInt);
                                        finish();
                                    }
                                });
                            }
                        });
                    }


                } else if (code == 501) {
                    mLv_contact.setEmptyView(mRl_nohas);
                }
            }

            @Override
            public void onFailure(String error) {
                Toast.makeText(HasContactActivity.this, "获取联系人失败", Toast.LENGTH_LONG).show();
            }

        });

        mLv_contact.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(HasContactActivity.this, ModifyContactActivity.class);
                intent.putExtra("id", id);
                intent.putExtra("name", dataBean.getName());
                intent.putExtra("wallet_addr", dataBean.getWallet_addr());
                intent.putExtra("coin_symbol", dataBean.getCoin_symbol());
                intent.putExtra("mobile", dataBean.getMobile());
                intent.putExtra("email", dataBean.getEmail());
                intent.putExtra("remark", dataBean.getRemark());
                startActivityForResult(intent, 4);
                finish();
                overridePendingTransition(R.anim.next_in_anim, R.anim.next_out_anim);

            }
        });

        mIb_back.setVisibility(View.VISIBLE);
        mV_title.setText(R.string.TheContact);

        mIb_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(HasContactActivity.this, AddContactActivity.class));
                finish();
                overridePendingTransition(R.anim.next_in_anim, R.anim.next_out_anim);

            }
        });
        mIb_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        mNohas_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mBt_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), AddContactActivity.class));
                finish();
                overridePendingTransition(R.anim.next_in_anim, R.anim.next_out_anim);

            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(R.anim.pre_in_anim, R.anim.pre_out_anim);

    }
    public void setStatuColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(android.R.color.white));
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        setMiuiStatusBarDarkMode(this, true);
        setMiuiStatusBarDarkMode(this, true);
    }
    //设置成白色的背景，字体颜色为黑色。
    public static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
        Class<? extends Window> clazz = activity.getWindow().getClass();
        try {
            int darkModeFlag = 0;
            Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            darkModeFlag = field.getInt(layoutParams);
            Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
            extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean setMeizuStatusBarDarkIcon(Activity activity, boolean dark) {
        boolean result = false;
        if (activity != null) {
            try {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                activity.getWindow().setAttributes(lp);
                result = true;
            } catch (Exception e) {
            }
        }
        return result;
    }
}
